package cn.krui.project.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.DataArray;
import cn.krui.helper.bean.DataSingle;
import cn.krui.helper.db.DBHelper;
import cn.krui.helper.db.SQL;
import cn.krui.project.bean.system.sys_dept.sys_dept;
import cn.krui.project.service.system.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; 

/**
 * @program: project-system
 * @description: 部门模块
 * @author: zc
 * @create: 2019-10-10 16:05
 **/
@Api(tags = "管理-部门", description = "部门模块接口", hidden = true)
@RestController
@RequestMapping("/system/sysdept")
@CrossOrigin
public class SysDeptController extends SystemBaseController {

	@Autowired
	private SysDeptService sysDeptService;

	@Autowired
	private DBHelper dbHelper;
	
	private final String EQUALS_ZERO = "0";

	/**
	 * 部门列表
	 */
	@PostMapping(value = "/getSysDeptList")
	@ApiOperation(value = "获取部门列表", httpMethod = "POST")
	public AppResult<sys_dept> getSysDeptList(
			@ApiParam(value = "部门名称", required = false) @RequestParam(value = "dept_name", required = false) String dept_name,
			@ApiParam(value = "部门级别", required = false) @RequestParam(value = "level", required = false) String level,
			@ApiParam(value = "省", required = false) @RequestParam(value = "dept_pro", required = false) String dept_pro,
			@ApiParam(value = "市", required = false) @RequestParam(value = "dept_city", required = false) String dept_city,
			@ApiParam(value = "地址", required = false) @RequestParam(value = "dept_addr", required = false) String dept_addr,
			@ApiParam(value = "拉取数据对应的用户id", required = false) @RequestParam(value = "uid", required = false) Integer uid) {
		AppResult<sys_dept> app = new AppResult<sys_dept>();
		DataSingle<sys_dept> where = new DataSingle<sys_dept>();
		sys_dept sysDept = new sys_dept();
		if (!StringUtils.isEmpty(dept_name)) {
			sysDept.setDept_name(dept_name);
		}
		if (!StringUtils.isEmpty(level)) {
			sysDept.setLevel(Integer.parseInt(level));
		}
		if (!StringUtils.isEmpty(dept_pro)) {
			sysDept.setDept_pro(dept_pro);
		}
		if (!StringUtils.isEmpty(dept_city)) {
			sysDept.setDept_city(dept_city);
		}
		if (!StringUtils.isEmpty(dept_addr)) {
			sysDept.setDept_addr(dept_addr);
		}
		where.setDataSingle(sysDept);
		where.setExtend(uid);
		List<sys_dept> sysdeptsList = sysDeptService.selectSysDeptList(where);
		List<sys_dept> deptsList = new ArrayList<>();
		// 循环递归获取子解点数据
		for (sys_dept dep : sysdeptsList) {
			dep.setChildren(this.getChild(dep.getId(), dep.getDept_name()));
			deptsList.add(dep);
		}
		app.ok();
		DataArray<sys_dept> list = new DataArray<sys_dept>();
		list.setDataList(sysdeptsList);
		list.setDataCount(sysdeptsList.size());
		app.setDataArray(list);
		app.setMsg("获取成功");
		return app;
	}

	/**
	 * 获取子节点
	 * 
	 * @param id      父节点id
	 * @param allMenu 所有菜单列表
	 * @return 每个根节点下，所有子部门列表
	 */
	public List<sys_dept> getChild(Integer id, String pName) {
		List<sys_dept> list = new ArrayList<>();
		// 子菜单
		SQL.SelectSQL sql = SQL.select("*").from("qr_sys_dept").where("pid=#{id}").orderBy("sort desc")
				.addParameter("id", id);
		List<sys_dept> childList = dbHelper.select(sql, sys_dept.class);
		for (sys_dept nav : childList) {
			nav.setP_name(pName);
			nav.setChildren(this.getChild(nav.getId(), nav.getDept_name()));
			list.add(nav);
		}
		return list;
	}

	/**
	 * 添加部门列表
	 */
	@PostMapping(value = "/addSysDept")
	@ApiOperation(value = "添加部门列表", httpMethod = "POST")
	public AppResult<sys_dept> addSysDept(
			@ApiParam(value = "部门级别路径", required = false) @RequestParam(value = "dept_path", required = false) String dept_path,
			@ApiParam(value = "父级id", required = true) @RequestParam(value = "pid", required = true) String pid,
			@ApiParam(value = "部门名称", required = true) @RequestParam(value = "dept_name", required = true) String dept_name,
			@ApiParam(value = "部门所属省", required = false) @RequestParam(value = "dept_pro", required = false) String dept_pro,
			@ApiParam(value = "部门所属城市", required = false) @RequestParam(value = "dept_city", required = false) String dept_city,
			@ApiParam(value = "部门所属地址", required = false) @RequestParam(value = "dept_addr", required = false) String dept_addr,
			@ApiParam(value = "部门邮箱", required = false) @RequestParam(value = "dept_letter", required = false) String dept_letter,
			@ApiParam(value = "排序", required = true) @RequestParam(value = "sort", required = true) String sort,
			@ApiParam(value = "备注", required = false) @RequestParam(value = "note", required = false) String note,
			@ApiParam(value = "部门电话", required = false) @RequestParam(value = "mobile", required = false) String mobile) {
		AppResult<sys_dept> app = new AppResult<sys_dept>();
		sys_dept sysDept = new sys_dept();
		if (!StringUtils.isEmpty(dept_path)) {
			sysDept.setDept_path(dept_path);
		}
		if (!StringUtils.isEmpty(pid)) {
			if (EQUALS_ZERO.equals(pid)) {
				sysDept.setLevel(0);
			} else {
				SQL.SelectSQL sql = SQL.select("*").from("qr_sys_dept").where("id=#{id} limit 1").addParameter("id",
						pid);
				sys_dept dept = dbHelper.selectOne(sql, sys_dept.class);
				sysDept.setLevel(dept.getLevel() + 1);
			}
			sysDept.setPid(Integer.parseInt(pid));

		}
		if (!StringUtils.isEmpty(dept_name)) {
			SQL.SelectSQL sql = SQL.select("dept_name").from("qr_sys_dept").where("dept_name=#{dept_name}")
					.where("group_id=#{group_id}").addParameter("dept_name", dept_name)
					.addParameter("group_id", getUser().getUid());
			List<Map<String, Object>> num = dbHelper.select(sql);
			if (num.size() > 0) {
				app.no("");
				app.setMsg("部门名称重复，添加失败");
				return app;
			}
			sysDept.setDept_name(dept_name);
		}
		if (!StringUtils.isEmpty(dept_pro)) {
			sysDept.setDept_pro(dept_pro);
		}
		if (!StringUtils.isEmpty(dept_city)) {
			sysDept.setDept_city(dept_city);
		}
		if (!StringUtils.isEmpty(dept_addr)) {
			sysDept.setDept_addr(dept_addr);
		}
		if (!StringUtils.isEmpty(dept_letter)) {
			sysDept.setDept_letter(dept_letter);
		}
		if (!StringUtils.isEmpty(sort)) {
			sysDept.setSort(Integer.parseInt(sort));
		}
		if (!StringUtils.isEmpty(note)) {
			sysDept.setNote(note);
		}
		if (!StringUtils.isEmpty(mobile)) {
			sysDept.setMobile(mobile);
		}
		sysDept.setStatus(1);
		sysDept.setGroup_id(getUser().getUid());
		Integer i = sysDeptService.addSysDept(sysDept);
		if (i > 0) {
			app.ok();
			app.setMsg("添加成功");
		} else {
			app.no("");
			app.setMsg("添加失败");
		}
		return app;
	}

	/**
	 * 更改部门
	 */
	@PostMapping(value = "/updateSysDept")
	@ApiOperation(value = "更改部门", httpMethod = "POST")
	public AppResult<sys_dept> updateSysDept(
			@ApiParam(value = "部门级别路径", required = false) @RequestParam(value = "dept_path", required = false) String dept_path,
			@ApiParam(value = "父级id", required = false) @RequestParam(value = "pid", required = false) String pid,
			@ApiParam(value = "部门名称", required = false) @RequestParam(value = "dept_name", required = false) String dept_name,
			@ApiParam(value = "部门所属省", required = false) @RequestParam(value = "dept_pro", required = false) String dept_pro,
			@ApiParam(value = "部门所属城市", required = false) @RequestParam(value = "dept_city", required = false) String dept_city,
			@ApiParam(value = "部门所属地址", required = false) @RequestParam(value = "dept_addr", required = false) String dept_addr,
			@ApiParam(value = "部门邮箱", required = false) @RequestParam(value = "dept_letter", required = false) String dept_letter,
			@ApiParam(value = "排序", required = false) @RequestParam(value = "sort", required = false) String sort,
			@ApiParam(value = "备注", required = false) @RequestParam(value = "note", required = false) String note,
			@ApiParam(value = "部门电话", required = false) @RequestParam(value = "mobile", required = false) String mobile,
			@ApiParam(value = "状态", required = false) @RequestParam(value = "status", required = false) String status,
			@ApiParam(value = "主键id", required = true) @RequestParam(value = "id", required = true) String id) {
		AppResult<sys_dept> app = new AppResult<sys_dept>();
		sys_dept sysDept = new sys_dept();
		if (id.equals(pid)) {
			app.no("");
			app.setMsg("更新失败,上级部门不能与当前部门相同");
			return app;
		}
		if (!StringUtils.isEmpty(dept_path)) {
			sysDept.setDept_path(dept_path);
		}
		if (!StringUtils.isEmpty(pid)) {
			if (EQUALS_ZERO.equals(pid)) {
				sysDept.setLevel(0);
			} else {
				SQL.SelectSQL sql = SQL.select("*").from("qr_sys_dept").where("id=#{id} limit 1").addParameter("id",
						pid);
				sys_dept dept = dbHelper.selectOne(sql, sys_dept.class);
				sysDept.setLevel(dept.getLevel() + 1);
				if (pid.equals(dept.getPid() + "")) {
					app.no("");
					app.setMsg("更新失败,上级部门不能为当前部门的子部门");
					return app;
				}
			}
			sysDept.setPid(Integer.parseInt(pid));
		}
		if (!StringUtils.isEmpty(dept_name)) {
			SQL.SelectSQL sql = SQL.select("dept_name").from("qr_sys_dept")
					.where("dept_name=#{dept_name} and id!=#{id}").where("group_id=#{group_id}")
					.addParameter("dept_name", dept_name).addParameter("id", id)
					.addParameter("group_id", getUser().getUid());
			List<Map<String, Object>> num = dbHelper.select(sql);
			if (num.size() > 0) {
				app.no("");
				app.setMsg("部门名称重复，更新失败");
				return app;
			}
			sysDept.setDept_name(dept_name);
		}
		if (!StringUtils.isEmpty(dept_pro)) {
			sysDept.setDept_pro(dept_pro);
		}
		if (!StringUtils.isEmpty(dept_city)) {
			sysDept.setDept_city(dept_city);
		}
		if (!StringUtils.isEmpty(dept_addr)) {
			sysDept.setDept_addr(dept_addr);
		}
		if (!StringUtils.isEmpty(dept_letter)) {
			sysDept.setDept_letter(dept_letter);
		}
		if (!StringUtils.isEmpty(sort)) {
			sysDept.setSort(Integer.parseInt(sort));
		}
		if (!StringUtils.isEmpty(note)) {
			sysDept.setNote(note);
		}
		if (!StringUtils.isEmpty(mobile)) {
			sysDept.setMobile(mobile);
		}
		if (!StringUtils.isEmpty(status)) {
			sysDept.setStatus(Integer.parseInt(status));
		}
		sysDept.setId(Integer.parseInt(id));
		sysDept.setGroup_id(null);
		Integer i = sysDeptService.updateSysDept(sysDept);
		if (i > 0) {
			app.ok();
			app.setMsg("更新成功");
		} else {
			app.no("");
			app.setMsg("更新失败");
		}
		return app;
	}

	/**
	 * 删除系统部门
	 */
	@PostMapping(value = "/deleteSysDept")
	@ApiOperation(value = "删除系统部门", httpMethod = "POST")
	public AppResult<sys_dept> deleteSysDept(
			@ApiParam(value = "主键id", required = true) @RequestParam(value = "id", required = true) String id) {
		AppResult<sys_dept> app = new AppResult<sys_dept>();
		SQL.SelectSQL sql = SQL.select("id").from("qr_sys_dept").where("pid=#{id}").addParameter("id", id);
		List<sys_dept> list = dbHelper.select(sql, sys_dept.class);
		if (list.size() > 0) {
			app.no("");
			app.setMsg("存在子部门,删除失败");
			return app;
		}

		SQL.SelectSQL usersql = SQL.select("id").from("qr_sys_users").where("dept_id=#{id}").addParameter("id", id);
		List<Map<String, Object>> userlist = dbHelper.select(usersql);
		if (userlist.size() > 0) {
			app.no("");
			app.setMsg("部门存在用户,删除失败");
			return app;
		}

		Integer i = sysDeptService.deleteSysDept(id);
		if (i > 0) {
			app.ok();
			app.setMsg("删除成功");
		} else {
			app.no("");
			app.setMsg("删除失败");
		}

		return app;
	}

}
